package com.xqls.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xqls.bean.ExportBean;

public class DownloadAct {
	/**
	 * 
	 * @param request
	 * @param response
	 * @param list
	 *            需要存放在excel里面的数据
	 * @param columnNames
	 *            放在excel里面的列名
	 * @param keys
	 *            对应列名的bean的字段名
	 * @return
	 * @throws IOException
	 */
	public static void excelDownload(HttpServletRequest request, HttpServletResponse response, List<Map<String, Object>> list,
			String[] columnNames, String[] keys, String sheetName) throws IOException {
		try {
			download(request, response, list, columnNames, keys, sheetName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// return null;
	}

	private static void download(HttpServletRequest request, HttpServletResponse response,
			List<Map<String, Object>> list, String[] columnNames, String[] keys, String sheetName) throws IOException {
		String fileName = sheetName;
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		try {
			ExcelUtil.createWorkBook(list, keys, columnNames, sheetName).write(os);
		} catch (IOException e) {
			e.printStackTrace();
		}

		byte[] content = os.toByteArray();
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
		response.setContentLength(content.length);
		response.getOutputStream().write(content);
		response.getOutputStream().flush();
		response.getOutputStream().close();

		// return null;
	}

	public static void excelDownload(HttpServletRequest request, HttpServletResponse response, ExportBean exportBean)
			throws IOException {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		ExcelUtil.createWorkBook(exportBean).write(os);
		byte[] content = os.toByteArray();
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-Disposition","attachment;filename=" + new String((exportBean.getSheetName() + ".xls").getBytes(), "iso-8859-1"));
		response.setContentLength(content.length);
		response.getOutputStream().write(content);
		response.getOutputStream().flush();
		response.getOutputStream().close();

		// return null;
	}

	/**
	 * 将list里面的object转化为map
	 * 
	 * @param list
	 * @return
	 * @throws Exception
	 */
	
	private static List<Map<String, Object>> createExcelRecord(List<Map<String, Object>> list, String sheetName) throws Exception {
		if (list == null) {
			return null;
		}
		List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
		Map<String, Object> sheetNameMap = new HashMap<String, Object>();
		sheetNameMap.put("sheetName", sheetName);
		listMap.add(sheetNameMap);
		Field[] declaredFields = list.get(0).getClass().getDeclaredFields();
		for (Object obj : list) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			for (Field field : declaredFields) {
				field.setAccessible(true);
				if (field.get(obj) instanceof Date) {
					// map.put(field.getName(),
					// DateUtil.dateToStr((Date)field.get(obj), "yyyy-MM-dd"));
				} else {
					map.put(field.getName(), field.get(obj));
				}
			}
			listMap.add(map);
		}
		return listMap;
	}
}
